package bg.piss.fmi;

import java.rmi.RemoteException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.activation.DataHandler;

import org.apache.axiom.attachments.ByteArrayDataSource;
import org.apache.axis2.AxisFault;

import bg.piss.fmi.SourceControlManipulatorStub.Commit;
import bg.piss.fmi.SourceControlManipulatorStub.CommitDir;
import bg.piss.fmi.SourceControlManipulatorStub.CommitDirResponse;
import bg.piss.fmi.SourceControlManipulatorStub.CommitResponse;
import bg.piss.fmi.SourceControlManipulatorStub.Connect;
import bg.piss.fmi.SourceControlManipulatorStub.ConnectResponse;
import bg.piss.fmi.SourceControlManipulatorStub.Delete;
import bg.piss.fmi.SourceControlManipulatorStub.DeleteResponse;
import bg.piss.fmi.SourceControlManipulatorStub.List;
import bg.piss.fmi.SourceControlManipulatorStub.ListResponse;

public class TestSVNConnector {

    public static String URL = "https://svn-connector.googlecode.com/svn/trunk/";
    public static String USER_NAME = "asenov_18@abv.bg";
    public static String PASSWORD = "Gk7Yu5bb8pt3";

    /**
     * @param args
     */
    public static void main(String[] args) {
        testConnect();
        testList("");
        String output = testList("");
		Pattern p = Pattern.compile("^<Folder.*name\\\"(.*)\\\"");
		Matcher m = p.matcher(output);
		while(m.find()) {
			System.out.println(m.group(1));
		}
    }

    private static String testConnect() {
        try {
            SourceControlManipulatorStub stub = new SourceControlManipulatorStub();
            Connect connectMethod = new Connect();
            connectMethod.setVersionControlURI(URL);
            connectMethod.setUserName(USER_NAME);
            connectMethod.setPassword(PASSWORD);
            ConnectResponse resp = stub.connect(connectMethod);
            return "Result from connect: " + resp.get_return();
        } catch (AxisFault e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return "";
    }

    private static String testList(String pathToList) {
        try {
            SourceControlManipulatorStub stub = new SourceControlManipulatorStub();
            List listMethod = new List();
            listMethod.setVersionControlURI(URL);
            listMethod.setUserName(USER_NAME);
            listMethod.setPassword(PASSWORD);
            listMethod.setPathToList(pathToList);
            ListResponse resp = stub.list(listMethod);
            return "Result from list: " + resp.get_return();
        } catch (AxisFault e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return "";
    }

    private static void testDelete(String pathToDelete) {
        try {
            SourceControlManipulatorStub stub = new SourceControlManipulatorStub();
            Delete deleteMethod = new Delete();
            deleteMethod.setVersionControlURI(URL);
            deleteMethod.setUserName(USER_NAME);
            deleteMethod.setPassword(PASSWORD);
            deleteMethod.setPathToDelete(pathToDelete);
            DeleteResponse resp = stub.delete(deleteMethod);
            System.out.println("Result from Delete: " + resp.get_return());
        } catch (AxisFault e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private static void testCommitDir(String pathToDir) {
        try {
            SourceControlManipulatorStub stub = new SourceControlManipulatorStub();
            CommitDir commitDirMethod = new CommitDir();
            commitDirMethod.setVersionControlURI(URL);
            commitDirMethod.setUserName(USER_NAME);
            commitDirMethod.setPassword(PASSWORD);
            commitDirMethod.setPathToDir(pathToDir);
            CommitDirResponse resp = stub.commitDir(commitDirMethod);
            System.out.println("Result from Commit Dir: " + resp.get_return());
        } catch (AxisFault e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private static void testCommit(String pathToFile, byte[] content) {
        try {
            SourceControlManipulatorStub stub = new SourceControlManipulatorStub();
            Commit commitMethod = new Commit();
            commitMethod.setVersionControlURI(URL);
            commitMethod.setUserName(USER_NAME);
            commitMethod.setPassword(PASSWORD);
            commitMethod.setPathToFile(pathToFile);
            commitMethod.setContent(new DataHandler(new ByteArrayDataSource(content)));
            CommitResponse resp = stub.commit(commitMethod);
            System.out.println("Result from Commit: " + resp.get_return());
        } catch (AxisFault e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

}
